# -*- encoding: utf-8 -*-

"""
------------------------------------------
@File       : 11_has_cycle.py
@Author     : maixiaochai
@Email      : maixiaochai@outlook.com
@CreatedOn  : 2022/1/21 16:05
------------------------------------------
    链表中是否有环
"""


class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


def is_cycle(head):
    p1 = head
    p2 = head

    while p2 and p2.next:
        p1 = p1.next
        p2 = p2.next.next

        if p1 == p2:
            return True

    return False


def demo():
    node1 = Node(5)
    node2 = Node(3)
    node3 = Node(7)
    node4 = Node(2)
    node5 = Node(6)

    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    node5.next = node2

    print(is_cycle(node1))


if __name__ == '__main__':
    demo()
